<?php
require_once 'vendor/autoload.php';

// 初始化应用
$app = new \think\App();
$app->initialize();

echo "测试订单同步功能...\n";

// 获取Token
$loginData = [
    'username' => 'admin',
    'password' => '123456'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8000/api/auth/login');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($loginData));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Accept: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "登录测试:\n";
echo "HTTP状态码: $httpCode\n";
echo "响应: $response\n";

$loginResult = json_decode($response, true);
if ($loginResult && isset($loginResult['data']['token'])) {
    $token = $loginResult['data']['token'];
    echo "获取到Token: $token\n\n";
    
    // 测试同步状态API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost:8000/api/orders/sync/status');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $token,
        'Accept: application/json'
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    echo "同步状态测试:\n";
    echo "HTTP状态码: $httpCode\n";
    echo "响应: $response\n\n";
    
    // 测试连接API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost:8000/api/orders/sync/test');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $token,
        'Accept: application/json'
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    echo "连接测试:\n";
    echo "HTTP状态码: $httpCode\n";
    echo "响应: $response\n\n";
    
    // 测试同步API（小范围测试）
    $syncData = [
        'start_date' => date('Y-m-d', strtotime('-1 day')),
        'end_date' => date('Y-m-d'),
        'max_days' => 2,
        'batch_size' => 10
    ];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost:8000/api/orders/sync');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($syncData));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $token,
        'Content-Type: application/json',
        'Accept: application/json'
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    echo "同步测试:\n";
    echo "HTTP状态码: $httpCode\n";
    echo "响应: $response\n\n";
    
} else {
    echo "登录失败，无法获取Token\n";
} 